#!/bin/bash
set -e
set -x

: ${PACKAGE_VERSION:="v99.99.99"} # 默认值
PACKAGE_VERSION=${PACKAGE_VERSION:1} # 去掉 v

ls /mnt
echo "Unpacking BALTAM_CORE component ..."
mkdir -p deps/core

if [[ "$1" == "ubuntu16.04" ]]; then
    export CC=/usr/local/bin/gcc
    export CXX=/usr/local/bin/g++
fi

filename_core_os=/mnt/BALTAM_CORE-$BALTAM_CORE_VERSION-Linux-$1-core.zip
filename_dev_os=/mnt/BALTAM_CORE-$BALTAM_CORE_VERSION-Linux-$1-dev.zip
filename_core=/mnt/BALTAM_CORE-$BALTAM_CORE_VERSION-Linux-core.zip
filename_dev=/mnt/BALTAM_CORE-$BALTAM_CORE_VERSION-Linux-dev.zip
filename_builtin_runtime=builtin_runtime-${BUILTIN_RUNTIME_VERSION}-Linux-$1-main.zip
url_builtin_runtime="${CI_API_V4_URL}/projects/${BUILTIN_RUNTIME_PROJECT_ID}/packages/generic/builtin_runtime/${BUILTIN_RUNTIME_VERSION}/${filename_builtin_runtime}"

if [[ -f "${filename_core_os}" ]]; then
    echo " > ${filename_core_os}"
    unzip -q ${filename_core_os} -d deps/core/
else
    echo " > ${filename_core}"
    unzip -q ${filename_core} -d deps/core/
fi

if [[ -f "${filename_dev_os}" ]]; then
    echo " > ${filename_dev_os}"
    unzip -q ${filename_dev_os} -d deps/core/
else
    echo " > ${filename_dev}"
    unzip -q ${filename_dev} -d deps/core/
fi

curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" -vO "${url_builtin_runtime}"
unzip -q ${filename_builtin_runtime} -d deps/builtin_runtime/

echo "Compiling the code..."
mkdir -p "build" && cd "build"
cmake -DCMAKE_BUILD_TYPE=Release -DPACKAGE_NAME_SUFFIX="$1" -DCODEGEN_TOOLS_VER="${PACKAGE_VERSION}" ..
make -j4
echo "Compile complete."
make package
